//
//  WBNavigationController.swift
//  weibo
//
//  Created by HanXujie on 2017/4/9.
//  Copyright © 2017年 thinker. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        // 隐藏系统的导航
        navigationBar.isHidden = true
    }

    
    /// 重写push方法，所有的push操作都会调用此方法
    // viewController就是要push的设置他的左侧按钮作为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        // 如果不是栈底控制器才会隐藏，根控制器不需要处理
        if childViewControllers.count > 0 {
            // 隐藏底部的tabBar
            viewController.hidesBottomBarWhenPushed = true
            
            if let vc = viewController as? WBBaseViewController {
                
                var title = "返回"
                // 判断控制器的级数只有一个控制器的时候显示栈底控制器的标题
                if childViewControllers.count == 1 {
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                // 取出自定义的navitem,设置左侧按钮欧威返回按钮
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToparent),isBackButton:true)
                
            }
        }
        


        super.pushViewController(viewController, animated: true)
    }
    
    @objc fileprivate func popToparent(){
        popViewController(animated: true)
    }

}
